package com.yanqu.road.server.manager.activity.daomu.pvp.task;

import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivity;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivityMgr;
import com.yanqu.road.server.manager.activity.daomu.pvp.DaoMuActivityPvp;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

/**
 * 盗墓笔记pvp定时任务
 */
public class DaoMuPvpRoomTask extends GeneralTask {

    private long roomShrinkTime;

    private long roomCollapseTime;

    // 离线玩家列表
    private long checkOffLineUserListTime;

    @Override
    public void doRun() {
        Map<Integer, DaoMuActivity> activityMap = DaoMuActivityMgr.getActivityMap();
        if (activityMap == null || activityMap.isEmpty()) {
            // 无活动
            return;
        }

        //  次/1s
        if (System.currentTimeMillis() > roomShrinkTime) {
            roomShrinkTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(daoMuActivity.getDaoMuActivityConfig().getActivityInfo())) {
                        continue;
                    }
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.roomShrinkTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("roomShrinkTask error : ", e);
                }
            }
        }

        //  次/1s
        if (System.currentTimeMillis() > roomCollapseTime) {
            roomCollapseTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(daoMuActivity.getDaoMuActivityConfig().getActivityInfo())) {
                        continue;
                    }
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.roomCollapseTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("roomCollapseTask error : ", e);
                }
            }
        }

        //  次/5s
        if (System.currentTimeMillis() > checkOffLineUserListTime) {
            checkOffLineUserListTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS * 5;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(daoMuActivity.getDaoMuActivityConfig().getActivityInfo())) {
                        continue;
                    }
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.checkOffLineUserListTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("checkOffLineUserListTask error : ", e);
                }
            }
        }

    }
}
